/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.map;

import java.nio.Buffer;

/**
 *
 * @author Mitchel Strachan
 */
public abstract class AIntMap<B extends Buffer> extends Map<B> {

    protected AIntMap(int sideLength, Format format, boolean normalized) {
        this(sideLength, 1, format, normalized);
    }

    protected AIntMap(int sideLength, int components, Format format, boolean normalized) {
        super(sideLength, components, format);
        this.normalized = normalized;
    }

    public final int get(int p, int u, int v) {
        return getInt(p, u, v);
    }

    public final void set(int p, int u, int v, int value) {
        setInt(p, u, v, value);
    }

    public final int get(int p, int u, int v, int component) {
        return getInt(index(p, u, v) + component);
    }

    public final void set(int p, int u, int v, int component, int value) {
        setInt(index(p, u, v) + component, value);
    }
    private final boolean normalized;

    public final boolean isNormalized() {
        return normalized;
    }
    
    public int get(int index) {
        return get(index, 0);
    }

    public int get(int index, int component) {
        if (component < 0 || component >= components) {
            throw new IllegalArgumentException("component must be between 0 and " + components);
        }
        return getInt(index * components + component);
    }

    @Override
    public final float getFloat(int bufferIndex) {
        if (!normalized) {
            return getInt(bufferIndex);
        }
        return ((float) getInt(bufferIndex) - format.min) / (format.span);
    }

    @Override
    public final void setFloat(int bufferIndex, float value) {
        if (!normalized) {
            setInt(bufferIndex, (int) value);
        } else {
            setInt(bufferIndex, (int) (value * format.span + format.min));
        }
    }
}
